//
//  LRPhotoBrowserDismissAnimation.swift
//  LRWeibo
//
//  Created by 雷路荣 on 16/1/2.
//  Copyright © 2016年 leilurong. All rights reserved.
//

import UIKit

class LRPhotoBrowserDismissAnimation: NSObject, UIViewControllerAnimatedTransitioning {
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.25
    }
    // 关闭时的动画
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        // 获取modal出来的控制器
        let fromVc = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as! LRPhotoBrowserController
        // 获取到modal出来的控制器的view
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        // 获取过渡视图
        let tempImageView = fromVc.dismissTempImageView()
        // 添加到容器视图
        transitionContext.containerView()?.addSubview(tempImageView)
        // 隐藏collectionView
        fromVc.collectionView.hidden = true
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            fromView?.alpha = 0
            // 将过渡视图缩放到目标位置(缩略图)
            tempImageView.frame = fromVc.dismissTargetRect()
            }) { (_) -> Void in
                //fromView?.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
    

}
























































